﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Follow.Business;

namespace Follow
{
    public partial class _Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                ViewFollowBO bo = new ViewFollowBO();
                Model.ViewFollowUp cls = new Model.ViewFollowUp();

                Model.Funcionario funcionario = new Model.Funcionario();
                FuncionarioBO boFuncionario = new FuncionarioBO();

                Model.Contato contato = new Model.Contato();
                ContatoBO boContato = new ContatoBO();


                if (boFuncionario.findByIdUsuario(Convert.ToInt32(Session["idUsuario"])) != null)
                {

                    funcionario = boFuncionario.findByIdUsuario(Convert.ToInt32(Session["idUsuario"]));
                    gvPropostasPendentes.DataSource = bo.findPropostaPendente(funcionario.IDFuncionario, funcionario.TipoFunc);
                    gvPropostasPendentes.DataBind();

                    if (gvPropostasPendentes.Rows.Count == 0)
                        lblMsgGrid.Visible = true;
                    else
                        lblMsgGrid.Visible = false;
                }
                else if (boContato.findByUsuarioContato(Convert.ToInt32(Session["idUsuario"])) != null)
                {
                    contato = boContato.findByUsuarioContato(Convert.ToInt32(Session["idUsuario"]));
                    gvPropostasPendentes.DataSource = bo.findPropostaPendenteContato(contato.IDCliente);
                    gvPropostasPendentes.DataBind();

                    if (gvPropostasPendentes.Rows.Count == 0)
                        lblMsgGrid.Visible = true;
                    else
                        lblMsgGrid.Visible = false;
                }
                else
                {
                    lblMsgGrid.Visible = true;
                }
            }
        }

        protected void gvPropostasPendentes_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string[] ids = e.CommandArgument.ToString().Split(';');

            string idFollowUp = ids[0];
            string idProposta = ids[1];

            if (e.CommandName == "Atualizar")
            {
                Response.Redirect("~/Cadastros/FollowUp.aspx?idProposta=" + idProposta + "&idFollowUp=" + idFollowUp);
            }
        }

        protected void gvPropostasPendentes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkbAtualizar = (LinkButton)e.Row.FindControl("lkbAtualizar");
                lkbAtualizar.CommandArgument = DataBinder.Eval(e.Row.DataItem, "IDFollowUp").ToString() + ";" + DataBinder.Eval(e.Row.DataItem, "IDProposta").ToString();
            }
        }
    }
}
